$(document).ready(function(){
	// Initialize code here
	wdxReplaceSrcIfNecessary("script");	// this is temp solution, do it on server side ?
	wdxReplaceSrcIfNecessary("img");
	wdxReplaceSrcIfNecessary("iframe");
	wdxReplaceHrefIfNecessary("link");
	
	wdxCreateWdxDisplay();
});

//-------------------------------------------------------------
// Proxy Link Functions
//-------------------------------------------------------------
var wdxConfig = new Object();
wdxConfig.magicIdentifier = 'localhost:8080';
wdxConfig.domain = /^(http|https):\/\/([a-z-.0-9]+)[\/]{0,1}/i.exec(wdxProxyUrl)[2];
wdxConfig.rootPath = /^(http|https):\/\/([a-z-.0-9\/]+)/i.exec(wdxProxyUrl.substring(0, wdxProxyUrl.lastIndexOf('/')))[2];

function wdxReplaceSrcIfNecessary(elementTag) {
	$(elementTag).each(function(){
		if(!this.src) return;
		if(this.src.indexOf(wdxConfig.magicIdentifier) >= 0)
			this.src = wdxReplaceWithRealPath(this.src);
	})
}

function wdxReplaceHrefIfNecessary(elementTag) {
	$(elementTag).each(function(){
		if(!this.href) return;
		if(this.href.indexOf(wdxConfig.magicIdentifier) >= 0)
			this.href = wdxReplaceWithRealPath(this.href);
	})
}

function wdxReplaceWithRealPath(url) {
	var elements = /^(http|https):\/\/([a-z-.:0-9]+)[\/](.+)/i.exec(url);
	var originalPath = elements[1] + "://" + wdxConfig.domain + "/" + elements[3];
	
	elements = /^(http|https):\/\/([a-z-.:0-9]+)\/wedaex\/(.+)/i.exec(url);
	if(elements != null) {	// use root path instead of domain
		originalPath = elements[1] + "://" + wdxConfig.rootPath + "/" + elements[3];
	}
	return originalPath;
}


//-------------------------------------------------------------
// Selection Functions
//-------------------------------------------------------------
function wdxCreateWdxDisplay() {
	$('#wdxDisplay').remove();
	
	var wdxDisplay =
		'<div id="wdxDisplay">' +
		'<input id="wdxSelectButton" type="button" value="Click To Select Element..."></input>' +
		'<input id="wdxConstructRegExpButton" type="button" value="Click To Construct Regular Expression..."></input>' +
		'</div>';

	$(wdxDisplay).appendTo('body');

	$('#wdxSelectButton').click(function(){
		this.value = 'Please Select...';
		wdxAddSelectEventToBody();
	});	
	
	$('#wdxConstructRegExpButton').click(function(){
		window.location = 'constructregexp.wdx';
	});
}

function wdxAddSelectEventToBody() {
	$('body').click(function(event){
		if($(event.target).parents('#wdxDisplay').length > 0)
			return false;	// ignore elements in wdxDisplay div

		// store and display the selected element in wdxDisplay
		wdxConfig.selectedElement = event.target.innerHTML
		$('<pre>Selected Element: ' + wdxConfig.selectedElement + '</pre>').appendTo('#wdxDisplay');

		// remvoe select event from body
		$('body').unbind('click');

		// change Select Button to Clear Button
		$('#wdxSelectButton').get(0).value = 'Clear Selection';
		$('#wdxSelectButton').unbind('click');
		$('#wdxSelectButton').click(function(){
			wdxCreateWdxDisplay();
		});
		
		// add button to select text within the element
		$('<input id="wdxSelectTextButton" type="button" value="Select Text and Click Me...">').appendTo('#wdxDisplay');
		$('#wdxSelectTextButton').click(function(){
			wdxConfig.selectedText = wdxGetSelectionText();
			$('<p> Selected Text: ' + wdxConfig.selectedText + '</p>').appendTo('#wdxDisplay');
			wdxCreateWdxSubmitForm();
		});
		
		return false;
	});
}

function wdxGetSelectionText() {
	if (window.getSelection) {
		return window.getSelection();
	}
	else if (document.getSelection) {
		return document.getSelection();
	}
	else if (document.selection) {
		return document.selection.createRange().text;
	}
	else return null;
}

/**
 * @deprecated not used for now
 */
function wdxPopUpSource(URL) {
	id = 'source';
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600');");
}

//-------------------------------------------------------------
// Generate Functions
//-------------------------------------------------------------
function wdxCreateWdxSubmitForm() {
	var wdxForm =
		'<div id="wdxForm">' +
		'<form action="generate.wdx">' +
		'<input type="hidden" name="selectedElement" value="' + encodeURIComponent(wdxConfig.selectedElement) + '" />' +
		'<input type="hidden" name="selectedText" value="' + encodeURIComponent(wdxConfig.selectedText) + '" />' +
		'<input type="submit" name="submit" value="Generate by Regular Expression" />' +
		'</form>' +
		'</div>';

	$(wdxForm).appendTo('#wdxDisplay');
}